package oocl.ita.eordering.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import oocl.ita.eordering.dao.FoodDao;
import oocl.ita.eordering.po.Food;

@Repository("fd")
public class FoodDaoImpl implements FoodDao {
	@PersistenceContext(name = "un")
	private EntityManager em;

	@Override
	public int addFood(Food f) {
		em.persist(f);
		return f.getfId();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Food> findFoodsByMId(Integer mId) {
		String jpql="from Food f where f.mId=:mId";
		return em.createQuery(jpql).setParameter("mId", mId).getResultList();
	}

//	@Override
//	public List<Food> findRecommendFood(Integer mId, Integer fStatus) {
//		// TODO Auto-generated method stub
//		return null;
//	}

	@Override
	public int deleteFood(Integer fId) {
		Food f=em.getReference(Food.class, fId);
		em.remove(f);
		return 1;
	}

	@Override
	public int updateFood(Integer fId, String fName, double fPrice, String fViewPath) {
		Food food=em.getReference(Food.class, fId);
		food.setfName(fName);
		food.setfPrice(fPrice);
		food.setfViewPath(fViewPath);
		
		return 1;
	}

	@Override
	public boolean updateStatus(Integer fId) {
		Food f=em.getReference(Food.class, fId);
		f.setfStatus(2);
		return true;
	}

	

}
